var editor1;
var editor2;
var editor3;
var editor;
var editorBannerImage;
var ke;
window.addMarkImageHtml=function(){
//	var mark='<input type="checkbox" checked="true" name="mark" style="vertical-align:middle;"/><span style="vertical-align:middle;">是否给图片打水印</span>';
//	$("form.ke-upload-area.ke-form .ke-dialog-row").after(mark);		
}


$(function(){
	var ke=KindEditor.editor({
		//allowFileManager: true,
		allowFlashUpload: true
	});
	$('.image').on("click", function() { //通用的更改自己，图片点击上传图片
		if(!window.noImage && window.noImage != undefined) return;
		var self = $(this);
		ke.loadPlugin('image', function() {
			ke.plugin.imageDialog({
				imageUrl: self.val(),
				clickFn: function(url, title, width, height, border, align) {
					//self.val(url);
					ke.hideDialog();
					self.attr("src", url);
					if(window.imageSrcChange)window.imageSrcChange(url, self);
					var name = self.attr('name');
					var input=self.next("input");
					if(input.length){
						input.val(url).attr('name',name);
					}else{
						self.after('<input type="hidden" name="'+name+'" value="'+url+'"/>');
					}
				}

			});
			//动态添加水印开关
			window.addMarkImageHtml();
		});
	}).each(function(){
		var e=$(this);
		var name=e.attr('name');
		var url=e.attr('src');
		e.after('<input type="hidden" name="'+name+'" value="'+url+'"/>');
	});
	
	//自动加载全部
	$i=0;
	$('.ktextarea').each(function(){
		KindEditor.create("#ktextarea-"+$i, {
			minHeight: 500,
			allowFileManager: true, //开启文件管理器
	
			//autoHeightMode: true, //自动调整高度
			afterCreate: function() {
				//this.loadPlugin('autoheight');
			},
	
			afterChange: function() {
				//    K('.word_count1').html(this.count());
				//    K('.word_count2').html(this.count('text'));
			}
		});
		console.log(editor)
		$i++;
	});
});



/*
KindEditor.ready(function(K) {
	
	
	

	editor2 = K.editor({
		allowFileManager: true,
		allowFlashUpload: true
	});

	K('#upload-logo').click(function() {

		editor2.loadPlugin('image', function() {
			editor2.plugin.imageDialog({
				imageUrl: K('#upload-logo').val(),
				clickFn: function(url, title, width, height, border, align) {
					K('#upload-logo').val(url);
					editor2.hideDialog();
					$('#upload-logo').next().attr("src", url);
				}

			});
			//动态添加水印开关
			window.addMarkImageHtml();
		});
	});

	editor3 = K.editor({
		allowFileManager: true,
		allowFlashUpload: true
	});
	K('#image1').click(function() {
		editor3.loadPlugin('image', function() {
			editor3.plugin.imageDialog({
				imageUrl: K('#url1').val(),
				clickFn: function(url, title, width, height, border, align) {
					K('#url1').val(url);
					editor3.hideDialog();
					$('#url1').parent().next().html("<img src='" + url + "' style='height:10em' alt='无法加载图片'/>");
				}

			});
			//动态添加水印开关
			window.addMarkImageHtml();
		});
	});
	
	
	
	//默认图片更改函数
	window.imageSrcChange = function(url, input) {
		input.next().val(url);
	}

	ke = K.editor({
		allowFileManager: true,
		allowFlashUpload: true
	});
	

	//与JQ选择器混用，添加轮播图片
	editorBannerImage = K.editor({
		allowFileManager: true,
	});

	$(".banner-image-show").click(function() {

		var t = $(this);
		editorBannerImage.loadPlugin('image', function() {
			editorBannerImage.plugin.imageDialog({
				imageUrl: t.prev().val(),
				imageTitle: t.next().val(),
				clickFn: function(url, title, width, height, border, align) {
					t.attr("src", url);

					title = $("#remoteTitle").val();
					t.attr("title", title);
					t.prev().val(url);
					t.next().val(title);
					t.attr("title", title);
					editorBannerImage.hideDialog();
				}
			});
			//动态添加水印开关
			window.addMarkImageHtml();
		});
		$("[for='remoteTitle']").text("图片链接");
		var v = $("#remoteTitle").val();
		var hrefTextarea = $('<textarea type="text" id="remoteTitle" class="ke-input-text" name="title" value="" ' +
			'style="width:300px;height:4em;display:inline-block;vertical-align:top;margin-top:5px;"></textarea>');
		$("#remoteTitle").replaceWith(hrefTextarea);
		hrefTextarea.val(v);
	});

	editor1 = K.create('#flash', {
		minHeight: 300,
		allowFileManager: true, //开启文件管理器
		items: ['source', '|', 'flash', '|', 'image'],
	});
	
	
	
	
	
	
	
	
	
	
	K('input[name=getHtml]').click(function(e) {
		alert(editor.html());
	}); //获取编辑器HTML
	K('input[name=getText]').click(function(e) {
		alert(editor.text());
	}); //获取编辑器文本

	K('input[name=isEmpty]').click(function(e) {
		alert(editor.isEmpty());
	}); //判断编辑器是否为空

	K('input[name=selectedHtml]').click(function(e) {
		alert(editor.selectedHtml());
	}); //选择辑器HTML

	K('input[name=setHtml]').click(function(e) {
		editor.html('<h3>Hello KindEditor</h3>');
	}); //设置编辑器HTML

	K('input[name=setText]').click(function(e) {
		editor.text('<h3>Hello KindEditor</h3>');
	}); //设置编辑器文本

	K('input[name=insertHtml]').click(function(e) {
		editor.insertHtml('<strong>插入HTML</strong>');
	}); //插入编辑器HTML
	K('input[name=appendHtml]').click(function(e) {
		editor.appendHtml('<strong>添加HTML</strong>');
	});

	K('input[name=clear]').click(function(e) {
		editor.html('');
	}); //清除编辑器内容
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
});*/